<?php
namespace app\know\model;

use app\know\model\KnowBaseModel;
use app\know\model\QuestionContentModel;
use app\know\model\QuestionOptionModel;

class QuestionModel extends KnowBaseModel
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'know_question';

    public function getQuestion($id = '', $uuid = '')
    {
        $where = ['is_delete' => 0, 'id' => $id];
        if ($id)
        {
            $where['id'] = $id;
        }
        else if ($uuid)
        {
            $where['uuid'] = $uuid;
        }

        $row = $this->where($where)->find();
        if (empty($row)) 
        {
            return [];
        }
        $content = QuestionContentModel::where('question_uuid', $row['uuid'])->find();
        if (empty($content)) 
        {
            return [];
        }

        $options = QuestionOptionModel::where('question_uuid', $row['uuid'])->select();
        foreach($options as $k1 => $v1)
        {
            $v1['content'] = $this->htmlDecode($v1['content']);
        }

        $content['content'] = $this->htmlDecode($content['content']);
        $content['option'] = $options;
        $content['answer'] = $this->htmlDecode($content['answer']);
        $content['text_analysis'] = $this->htmlDecode($content['text_analysis']);

        $w = ['is_delete' => 0, 'parent_uuid' => $row['uuid']];
        $rows = $this->where($w)->select()->toArray();
        
        $uuids = [];
        foreach($rows as $v)
        {
            array_push($uuids, $v['uuid']);
        }

        $children = [];
        if (count($uuids) > 0)
        {
            $contents = QuestionContentModel::where('question_uuid', 'in', $uuids)->select()->toArray();
            $ops = QuestionOptionModel::where('question_uuid', 'in', $uuids)->select()->toArray();
            foreach($ops as &$ps)
            {
                $ps['content'] = $this->htmlDecode($ps['content']);
            }

            foreach($contents as $ck => &$co)
            {
                $co['option'] = [];
                foreach($ops as $pk => &$ps)
                {
                    if ($co['question_uuid'] === $ps['question_uuid'])
                    {
                        array_push($co['option'], $ps);
                    }
                }
            }

            foreach($rows as $v)
            {
                foreach($contents as $c)
                {
                    if ($c['question_uuid'] === $v['uuid'])
                    {
                        $c['content'] = $this->htmlDecode($c['content']);
                        $c['answer'] = $this->htmlDecode($c['answer']);
                        $c['text_analysis'] = $this->htmlDecode($c['text_analysis']);

                        array_push($children, ['base' => $v, 'content' => $c ]);
                        break;
                    }
                }
            }
        }

        return ['base' => $row, 'content' => $content, 'children' => $children ];
    }

    public function getQuestionMerge($id, $uuid)
    {
        $row = $this->getQuestionItem($id, $uuid);
        if (empty($row)) 
        {
            return [];
        }

        $w = ['is_delete' => 0, 'parent_uuid' => $row['uuid']];
        $rows = QuestionModel::where($w)->select()->toArray();
        
        foreach($rows as $v)
        {
            $c = $this->getQuestionItem($v['id']);
            array_push($row['children'], $c);
        }
        return $row;
    }

    public function getQuestionItem($id, $uuid)
    {
        $where = ['is_delete' => 0];
        if ($id)
        {
            $where['id'] = $id;
        }
        else if ($uuid)
        {
            $where['uuid'] = $uuid;
        }

        $row = $this->where($where)->find();
        if (empty($row)) 
        {
            return [];
        }
        $content = QuestionContentModel::where('question_uuid', $row['uuid'])->find();
        if (empty($content)) 
        {
            return [];
        }

        $options = QuestionOptionModel::where('question_uuid', $row['uuid'])->select();
        foreach($options as &$v1)
        {
            $v1['content'] = $this->htmlDecode($v1['content']);
        }

        $row['content'] = $this->htmlDecode($content['content']);
        $row['option'] = $options;
        $row['answer'] = $this->htmlDecode($content['answer']);
        $row['text_analysis'] = $this->htmlDecode($content['text_analysis']);
        $row['video_analysis'] = $this->htmlDecode($content['video_analysis']);
        $row['remark'] = $content['remark'];
        $row['children'] = [];

        return $row;
    }


}